const MYSQL = require("../../sql/mysql");
const MYSQLConfig = require("../../sql/database");
const SQLName = MYSQLConfig.datasheetName.user_token;
/**
 * 获取token记录信息
 * @param {object} params
 * @param {string} params.secretKey
 * @return {Promise<{code:number,msg:string,data:object} | {code:number,msg:string}>}
 */
module.exports = async (params = {}) => {
  try {
    let { secretKey, type = 0 } = params;

    if (!secretKey) {
      throw { code: 500, msg: "操作失败,密钥不能为空" };
    }

    let query = `SELECT * FROM ${SQLName} WHERE secretKey = '${secretKey}' AND type = ${type}`;

    let secretKeyList = null;

    try {
      secretKeyList = await MYSQL.queryPromise(query);
    } catch (error) {

      console.log("==MYSQL==========================");
      console.log(MYSQL);
      console.log("==error==========================");
      console.log(error);
      throw { code: 500, msg: "操作失败,在查询密钥记录时发生错误" };
    }
    if (secretKeyList.length) {
      return {
        code: 0,
        msg: "获取成功",
        data: secretKeyList[0],
      };
    } else {
      let msg = "";
      if (type == 0) {
        msg = "操作失败,token不存在";
      } else if (type == 1) {
        msg = "操作失败,cookie不存在";
      }
      throw { code: 500, msg, statusCode: 403 };
    }
  } catch (error) {
    throw error;
  }
};
